package cn.unknowpupil.init.service.system.impl;

import cn.unknowpupil.init.configure.mybatisplus.base.BasePage;
import cn.unknowpupil.init.domain.system.SysLoginInfo;
import cn.unknowpupil.init.mapper.system.SysLoginInfoMapper;
import cn.unknowpupil.init.service.system.ISysLoginInfoService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * Created at 2020-10-20 by  w10g <br>
 * Initialize SysLoginInfo  ... <br>
 * 系统访问记录 核心业务处理实现类
 *
 * @author unknowpupil
 * @since 0.0.1
 */
@Service
public class SysLoginInfoServiceImpl extends ServiceImpl<SysLoginInfoMapper, SysLoginInfo> implements ISysLoginInfoService {




    /**
     * 删除
     *
     * @param ids {@code Long}   SysLoginInfo ID
     * @return {@link SysLoginInfo}
     */
    @Override
    public boolean delete(Long[] ids) {
        return removeByIds(Stream.of(ids).collect(Collectors.toList()));
    }


    /**
     * 分页
     *
     * @param current      {@code int} 页码
     * @param size         {@code int} 笔数
     * @param sysLoginInfo {@link SysLoginInfo}
     * @return {@code IPage<SysLoginInfo>}
     */
    @Override
    public IPage<SysLoginInfo> page(int current, int size, SysLoginInfo sysLoginInfo) {
        IPage<SysLoginInfo> page = new Page<>(current, size);

        LambdaQueryWrapper<SysLoginInfo> wrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(sysLoginInfo.getIpaddr())) {
            wrapper.like(SysLoginInfo::getIpaddr, sysLoginInfo.getIpaddr());
        }
        if (StringUtils.isNotBlank(sysLoginInfo.getIpaddr())) {
            wrapper.like(SysLoginInfo::getIpaddr, sysLoginInfo.getIpaddr());
        }
        if (StringUtils.isNotBlank(sysLoginInfo.getStatus())) {
            wrapper.eq(SysLoginInfo::getStatus, sysLoginInfo.getStatus());
        }
        if (StringUtils.isNotBlank(sysLoginInfo.getUserName())) {
            wrapper.like(SysLoginInfo::getUserName, sysLoginInfo.getUserName());
        }

        if (StringUtils.isNotBlank(sysLoginInfo.getUserName())) {
            wrapper.like(SysLoginInfo::getUserName, sysLoginInfo.getUserName());
        }
        wrapper.apply(StringUtils.isNotBlank(sysLoginInfo.getBeginTime()),
                "date_format (login_time,'%Y-%m-%d') >= date_format('" + sysLoginInfo.getBeginTime() + "','%Y-%m-%d')");

        wrapper.apply(StringUtils.isNotBlank(sysLoginInfo.getEndTime()),
                "date_format (login_time,'%Y-%m-%d') <= date_format('" + sysLoginInfo.getEndTime() + "','%Y-%m-%d')");
        wrapper.orderByDesc(SysLoginInfo::getId);
        return super.page(page, wrapper);
    }

    @Override
    public boolean create(SysLoginInfo loginInfo) {

        return save(loginInfo);
    }

}
